package com.demo.controller;

import com.demo.service.RecommendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.mahout.cf.taste.common.TasteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: LiuYuanQi
 * @Date: 2020/4/15 9:58
 */
@Slf4j
@RestController
@RequestMapping("/recommend")
@Api(description = "推荐")
public class RecommendController {

    @Autowired
    private RecommendService recommendService;

    @ApiOperation(value = "基于用户推荐")
    @PostMapping(value = "/forUser")
    public Object forUser(){
        try {
            return recommendService.forUser();
        } catch (Exception e) {
            return e;
        }
    }

    @ApiOperation(value = "基于物品推荐", notes = "用户id 0-4 ，商品id 0-6")
    @PostMapping(value = "/forItem")
    public Object forItem(@RequestParam(value = "userId", defaultValue="1", required = false) @ApiParam("当前用户id")  Long userId,
                                    @RequestParam(value = "itemId", defaultValue="5", required = false) @ApiParam("浏览的商品id") Long itemId,
                                    @RequestParam(value = "howMany", defaultValue="2", required = false) @ApiParam("推荐多少个商品")  Integer howMany){
        try {
            return recommendService.forItem(userId, itemId, howMany);
        } catch (Exception e) {
            return e;
        }
    }

    @ApiOperation(value = "根据训练数据量求差值评估率", notes = "evaluate 为0-1.0之间，意思为取百分之多少数据训练。")
    @PostMapping(value = "/forEvaluate")
    public Object forEvaluate(@RequestParam(value = "evaluate", defaultValue="0.5", required = false) @ApiParam("取百分之多少数据训练")  Double evaluate){
        try {
            return recommendService.forEvaluate(evaluate);
        } catch (Exception e) {
            return e;
        }
    }

    @ApiOperation(value = "搜集推荐性能相关的指标，计算Precision(准确率)、Recall(召回率)", notes = "evaluate 为0-1.0之间，意思为取百分之多少数据训练。")
    @PostMapping(value = "/precisionAndRecall")
    public Object precisionAndRecall(@RequestParam(value = "precisionAndRecallNum", defaultValue="4", required = false) @ApiParam("计算几个结果时的查准率和召回率")  Integer precisionAndRecallNum){
        try {
            return recommendService.precisionAndRecall(precisionAndRecallNum);
        } catch (Exception e) {
            return e;
        }
    }

}